/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2016-11-22
 * 修改历史 :
 * 1. [2016-11-22]创建文件 by chenxin
 */
package com.sinosoft.sinopay.paytrade.biz.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sinosoft.sinopay.paytrade.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.sinocloud.platform.base.SinoBaseException;
import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.context.SpringContext;
import com.sinosoft.sinocloud.platform.common.exception.biz.SinoBizException;
import com.sinosoft.sinocloud.platform.common.util.DateUtil;
import com.sinosoft.sinocloud.platform.common.web.pagequery.PageGrid;
import com.sinosoft.sinocloud.platform.common.web.pagequery.PageQueryModel;
import com.sinosoft.sinopay.api.common.dto.pay.PaymentInfoDetailDTO;
import com.sinosoft.sinopay.api.common.dto.query.PayInfoQryParam;
import com.sinosoft.sinopay.api.pay.dto.AllinpayQryReqDTO;
import com.sinosoft.sinopay.api.pay.dto.AllinpayQryRespDTO;
import com.sinosoft.sinopay.api.pay.service.InteraAllinPayQryApiService;
import com.sinosoft.sinopay.merchants.dto.MerchantSignInfo;
import com.sinosoft.sinopay.merchants.facade.MerchantInfoFacadeService;
import com.sinosoft.sinopay.paychannel.dto.PayThirdpayDTO;
import com.sinosoft.sinopay.paychannel.facade.PayChannelConfigFacadeService;
import com.sinosoft.sinopay.paytrade.biz.integration.dao.PayTradePageMapper;
import com.sinosoft.sinopay.paytrade.biz.integration.mapper.PayOrderMapper;
import com.sinosoft.sinopay.paytrade.biz.integration.mapper.PayTradeMapper;
import com.sinosoft.sinopay.paytrade.biz.service.PaymentTradeQryCoreService;
import com.sinosoft.sinopay.paytrade.generated.mapper.PayCodeMappingMapper;
import com.sinosoft.sinopay.paytrade.generated.mapper.PayTradeBatchMapper;
import com.sinosoft.sinopay.paytrade.generated.mapper.PayTradeMainMapper;
import com.sinosoft.sinopay.paytrade.generated.mapper.PayTradeNotifyRecordMapper;
import com.sinosoft.sinopay.paytrade.generated.mapper.PayTradeOfferLogMapper;
import com.sinosoft.sinopay.paytrade.generated.mapper.PayTradeOrderMapper;
import com.sinosoft.sinopay.paytrade.generated.mapper.PayTradePaymentBatchMapper;
import com.sinosoft.sinopay.paytrade.generated.mapper.RelaTradePaymentMapper;
import com.sinosoft.sinopay.paytrade.generated.model.PayCodeMapping;
import com.sinosoft.sinopay.paytrade.generated.model.PayCodeMappingExample;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradeBatch;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradeBatchExample;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradeMain;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradeMainExample;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradeNotifyRecord;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradeOfferLog;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradeOfferLogExample;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradePaymentBatch;
import com.sinosoft.sinopay.paytrade.generated.model.PayTradePaymentBatchExample;
import com.sinosoft.sinopay.paytrade.generated.model.RelaTradePayment;
import com.sinosoft.sinopay.paytrade.generated.model.RelaTradePaymentExample;

/**
 *【支付交易查询服务接口实现类】
 * @author chenxin
 * @date 2016-11-22 下午1:39 
 * @version V1.0
 */
@Service
public class PaymentTradeQryCoreServiceImpl extends SinoBaseService implements PaymentTradeQryCoreService {
    @Autowired
    private PayTradeMapper payTradeMapper;
    @Autowired
    private PayTradeOrderMapper payTradeOrderMapper;
    
    @Autowired
    private PayOrderMapper payOrderMapper;
    
    @Autowired
    private PayTradeBatchMapper payTradeBatchMapper;
	
	@Autowired
    private PayTradePaymentBatchMapper payTradePaymentBatchMapper;
    
	@Autowired
    private PayTradeOfferLogMapper payTradeOfferLogMapper;
	
	@Autowired
    private RelaTradePaymentMapper relaTradePaymentMapper;
	
	@Autowired
	private PayTradeMainMapper payTradeMainMapper;
	
	@Autowired
    private PayTradeNotifyRecordMapper payTradeNotifyRecordMapper;
	
	@Autowired
    private PayCodeMappingMapper payCodeMappingMapper;
	
	@Autowired
    private PayTradePageMapper payTradePageMapper;
	
	@Autowired
    private MerchantInfoFacadeService merchantInfoFacadeService;
	
	@Autowired
    private PayChannelConfigFacadeService payChannelConfigFacadeService;
	
    @Override
    public List<PaymentInfoDetailDTO> queryPaymentDetailListByParam(PayInfoQryParam queryParam) {
        return payTradeMapper.selectPaymentDetailListByParam(queryParam);
    }

    @Override
    public PaymentInfoDetailDTO queryPaymentOrderByPayNo(String payNo){
    	//进行DTO转换
    	PaymentRequestDTO tPaymentRequestDTO = payTradeMapper.selectPaymentOrderByPayNo(payNo);
    	PaymentInfoDetailDTO tPaymentInfoDetailDTO = new PaymentInfoDetailDTO();
    	tPaymentInfoDetailDTO.setMerchantCode(tPaymentRequestDTO.getMerchantCode());
    	tPaymentInfoDetailDTO.setBizNo(tPaymentRequestDTO.getBizNo());
    	tPaymentInfoDetailDTO.setSerialNo(tPaymentRequestDTO.getSerialNo());
    	tPaymentInfoDetailDTO.setBizType(tPaymentRequestDTO.getBizType());
    	tPaymentInfoDetailDTO.setPayAmount(tPaymentRequestDTO.getPayAmount());
    	tPaymentInfoDetailDTO.setAccountName(tPaymentRequestDTO.getPayAccount().getAccountName());
    	tPaymentInfoDetailDTO.setIdType(tPaymentRequestDTO.getPayAccount().getCardType());
    	tPaymentInfoDetailDTO.setIdNo(tPaymentRequestDTO.getPayAccount().getCardNo());
    	tPaymentInfoDetailDTO.setBankCode(tPaymentRequestDTO.getPayAccount().getBankCode());
    	tPaymentInfoDetailDTO.setBankName(tPaymentRequestDTO.getPayAccount().getBankName());
    	tPaymentInfoDetailDTO.setAccountNo(tPaymentRequestDTO.getPayAccount().getAccountNo());
    	tPaymentInfoDetailDTO.setMobile(tPaymentRequestDTO.getPayAccount().getMobile());
    	tPaymentInfoDetailDTO.setCreateDate(tPaymentRequestDTO.getCreateDate());
    	tPaymentInfoDetailDTO.setTradeDesc(tPaymentRequestDTO.getTradeDesc());
    	tPaymentInfoDetailDTO.setRemark(tPaymentRequestDTO.getRemark());
    	tPaymentInfoDetailDTO.setThirdpayCode(tPaymentRequestDTO.getThirdpayCode());
    	tPaymentInfoDetailDTO.setPayType(tPaymentRequestDTO.getPayType());
    	tPaymentInfoDetailDTO.setPayState(tPaymentRequestDTO.getPayStatus());
    	tPaymentInfoDetailDTO.setSid(tPaymentRequestDTO.getSid());
    	tPaymentInfoDetailDTO.setReqBatchNo(tPaymentRequestDTO.getReqBatchNo());
    	tPaymentInfoDetailDTO.setCertType(tPaymentRequestDTO.getCertType());
    	tPaymentInfoDetailDTO.setOpenBank(tPaymentRequestDTO.getOpenBank());
    	tPaymentInfoDetailDTO.setOpenBankArea(tPaymentRequestDTO.getOpenBankArea());
    	tPaymentInfoDetailDTO.setCurrency(tPaymentRequestDTO.getCurrency());
    	tPaymentInfoDetailDTO.setPayNo(tPaymentRequestDTO.getPayNo());
    	return tPaymentInfoDetailDTO;
    }

    @Override
	public List<Map<String, Object>> queryTranseOrderSidList(String reqBatchNo) {
		//根据 支付批次ID 查询关联四张表 交易订单Sid列表信息
		
		// 查询支付渠道主表 sid列表信息
		PayTradeMainExample tPayTradeMainExample = new PayTradeMainExample();
		tPayTradeMainExample.createCriteria().andBatchNoEqualTo(reqBatchNo);
		List <PayTradeMain> payTradeMainList= payTradeMainMapper.selectByExample(tPayTradeMainExample);
		
		
		// 查询支付交易订单批次 sid 列表信息
		PayTradeBatchExample tPayTradeBatchExample = new PayTradeBatchExample();
		tPayTradeBatchExample.createCriteria().andReqBatchNoEqualTo(reqBatchNo);
		List <PayTradeBatch> payTradeBatchList= payTradeBatchMapper.selectByExample(tPayTradeBatchExample);
		
		
		// 查询批量支付总表  sid列表信息
		PayTradePaymentBatchExample tPayTradePaymentBatchExample = new PayTradePaymentBatchExample();
		tPayTradePaymentBatchExample.createCriteria().andPayBatchNoEqualTo(reqBatchNo);
		List <PayTradePaymentBatch> payTradePaymentBatchList= payTradePaymentBatchMapper.selectByExample(tPayTradePaymentBatchExample);
		
		// 查询报盘记录表  sid列表信息
		PayTradeOfferLogExample tPayTradeOfferLogExample = new PayTradeOfferLogExample();
		tPayTradeOfferLogExample.createCriteria().andPayBatchNoEqualTo(reqBatchNo);
		List <PayTradeOfferLog> payTradeOfferLogList=  payTradeOfferLogMapper.selectByExample(tPayTradeOfferLogExample);
		
		Map map = new HashMap<String, Object>();  
		map.put("payTradeMainList", payTradeMainList);
		map.put("payTradeBatchList", payTradeBatchList);
		map.put("payTradePaymentBatchList", payTradePaymentBatchList);
		map.put("payTradeOfferLogList", payTradeOfferLogList);
		List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();  
		listMap.add(map);  
		return listMap;
	}
	
    @Override
	public List<PayTradeNotifyRecordDTO> queryPayTradeNotifyRecord(PayTradeNotifyRecordDTO cPayTradeNotifyRecord) {
		//查询参数添加 查询条件 判断处理
       /* if (!"SUCCESS".equals(cPayTradeNotifyRecord.getNotifyResult())) {
            cPayTradeNotifyRecord.setNotifyResult(notifyResult);
        }*/
		List<PayTradeNotifyRecord> tPayTradeNotifyRecordList = payTradeMapper.selectTradeNotifyRecordListByParam(cPayTradeNotifyRecord);  //根据订单号 查询订单信息
		List<PayTradeNotifyRecordDTO> tPayTradeNotifyRecordList2 = new ArrayList<PayTradeNotifyRecordDTO>();
		for (int i = 0; i < tPayTradeNotifyRecordList.size(); i++) {
		    PayTradeNotifyRecordDTO tPayTradeNotifyRecordDTO = new PayTradeNotifyRecordDTO();
	        tPayTradeNotifyRecordDTO.setMerchantCode(tPayTradeNotifyRecordList.get(i).getMerchantCode());
	        tPayTradeNotifyRecordDTO.setPayNo(tPayTradeNotifyRecordList.get(i).getPayNo());
	        tPayTradeNotifyRecordDTO.setNotifyUrl(tPayTradeNotifyRecordList.get(i).getNotifyUrl());
	        tPayTradeNotifyRecordDTO.setNotifyType(tPayTradeNotifyRecordList.get(i).getNotifyType());
	        tPayTradeNotifyRecordDTO.setNotifyTimes(tPayTradeNotifyRecordList.get(i).getNotifyTimes());
	        tPayTradeNotifyRecordDTO.setLimitNotifyTimes(tPayTradeNotifyRecordList.get(i).getLimitNotifyTimes());
	        tPayTradeNotifyRecordDTO.setNotifyResult(tPayTradeNotifyRecordList.get(i).getNotifyResult());
	        tPayTradeNotifyRecordList2.add(tPayTradeNotifyRecordDTO);
        }

	    return tPayTradeNotifyRecordList2;	
	}

	@Override
	public List<PaymentInfoDetailDTO> queryAllPayTransInfoForDay(String thirdpayCode) {
		//查询当日所有支付交易信息表数据 请求参数 渠道编码、当日
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
		String dateNow = dateFormater.format(DateUtil.getCurrentDate()).toString();
		
		PayInfoQryParam tPayInfoQryParam = new PayInfoQryParam();
		tPayInfoQryParam.setThirdpayCode(thirdpayCode);
		tPayInfoQryParam.setPayDate(dateNow);   //当前日期
		List<PaymentInfoDetailDTO> tPaymentTradeReqList = payTradeMapper.selectDayAllPayTransInfo(tPayInfoQryParam);
		return tPaymentTradeReqList;
	}

	@Override
	public List<PaymentTradeReqDTO> queryAllPayOrderBatchesOfDay(String thirdpayCode) {
		// 查询当日所有支付订单批次表数据
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
		String dateNow = dateFormater.format(DateUtil.getCurrentDate()).toString();
		
		PayInfoQryParam tPayInfoQryParam = new PayInfoQryParam();
		tPayInfoQryParam.setThirdpayCode(thirdpayCode);
		tPayInfoQryParam.setPayDate(dateNow);
		List<PayTradeBatch> tPayTradeBatchList = payTradeMapper.selectDayAllPayOrderBatches(tPayInfoQryParam);
		List<PaymentTradeReqDTO> tPaymentTradeReqList = new ArrayList<PaymentTradeReqDTO>();
		for (int i = 0; i < tPayTradeBatchList.size(); i++) {  //转换并返回
			PaymentTradeReqDTO tPaymentTradeReqDTO = new PaymentTradeReqDTO(); 
			tPaymentTradeReqDTO.setSid(tPayTradeBatchList.get(i).getSid());
            tPaymentTradeReqDTO.setReqBatchNo(tPayTradeBatchList.get(i).getReqBatchNo());
            tPaymentTradeReqDTO.setMerchantCode(tPayTradeBatchList.get(i).getMerchantCode());
            tPaymentTradeReqDTO.setPayFlag(tPayTradeBatchList.get(i).getPayFlag());
            tPaymentTradeReqDTO.setPayChannelId(tPayTradeBatchList.get(i).getPayChannelId());
            tPaymentTradeReqDTO.setTradeNum(tPayTradeBatchList.get(i).getTradeNum());
            tPaymentTradeReqDTO.setSumAmount(tPayTradeBatchList.get(i).getSumAmount());
            tPaymentTradeReqDTO.setResultCode(tPayTradeBatchList.get(i).getResultCode());
            tPaymentTradeReqDTO.setResultDesc(tPayTradeBatchList.get(i).getResultDesc());
            tPaymentTradeReqDTO.setDealDate(tPayTradeBatchList.get(i).getDealDate());
            tPaymentTradeReqDTO.setCreatorId(tPayTradeBatchList.get(i).getCreatorId());
            tPaymentTradeReqDTO.setCreateDate(tPayTradeBatchList.get(i).getCreateDate());
            tPaymentTradeReqDTO.setModifierId(tPayTradeBatchList.get(i).getModifierId());
            tPaymentTradeReqDTO.setModifyDate(tPayTradeBatchList.get(i).getModifyDate());
            tPaymentTradeReqList.add(tPaymentTradeReqDTO);
		}
		
		return tPaymentTradeReqList;
	}

	@Override
	public PaymentTradeReqDTO queryPayTransInfoBySid(Integer Sid) {
		PayTradeMain tPayTradeMain = payTradeMainMapper.selectByPrimaryKey(Sid); 
		PaymentTradeReqDTO tPaymentTradeReqDTO = new PaymentTradeReqDTO();
		tPaymentTradeReqDTO.setSid(tPayTradeMain.getSid());
		tPaymentTradeReqDTO.setSerialNo(tPayTradeMain.getSerialNo());
		tPaymentTradeReqDTO.setBizNo(tPayTradeMain.getBizNo());
		tPaymentTradeReqDTO.setBatchNo(tPayTradeMain.getBatchNo());
		tPaymentTradeReqDTO.setPayNo(tPayTradeMain.getPayNo());
		tPaymentTradeReqDTO.setMerchantCode(tPayTradeMain.getMerchantCode());
		tPaymentTradeReqDTO.setThirdpayCode(tPayTradeMain.getThirdpayCode());
		
		//查询对应的 商户名称 及支付渠道名称 
		MerchantSignInfo tMerchantSignInfo = merchantInfoFacadeService.getMerchantByCode(tPayTradeMain.getMerchantCode());
        PayThirdpayDTO tPayThirdpayDTO = payChannelConfigFacadeService.queryPayThirdpayMainByThirdpayCode(tPayTradeMain.getThirdpayCode());
        tPaymentTradeReqDTO.setMerchantName(tMerchantSignInfo.getMerchantName());
        tPaymentTradeReqDTO.setThirdpayName(tPayThirdpayDTO.getThirdpayName());
        
		tPaymentTradeReqDTO.setBizType(tPayTradeMain.getBizType());
		tPaymentTradeReqDTO.setPayWay(tPayTradeMain.getPayWay());
		tPaymentTradeReqDTO.setPayType(tPayTradeMain.getPayType());
		tPaymentTradeReqDTO.setPayStatus(tPayTradeMain.getPayStatus());
		tPaymentTradeReqDTO.setPayDate(tPayTradeMain.getPayDate());
		tPaymentTradeReqDTO.setTradeTimes(tPayTradeMain.getTradeTimes());
		tPaymentTradeReqDTO.setPayAmount(tPayTradeMain.getPayAmount());
		tPaymentTradeReqDTO.setPayAccountName(tPayTradeMain.getPayAccountName());
		tPaymentTradeReqDTO.setPayAccountMobile(tPayTradeMain.getPayAccountMobile());
		tPaymentTradeReqDTO.setPayBankName(tPayTradeMain.getPayBankName());
		tPaymentTradeReqDTO.setPayBankCode(tPayTradeMain.getPayBankCode());
		tPaymentTradeReqDTO.setPayAccountNo(tPayTradeMain.getPayAccountNo());
		tPaymentTradeReqDTO.setThirdPayNo(tPayTradeMain.getThirdPayNo());
		tPaymentTradeReqDTO.setCurrency(tPayTradeMain.getCurrency());
		tPaymentTradeReqDTO.setCheckDate(tPayTradeMain.getCheckDate());
		
		return tPaymentTradeReqDTO;
	}

	@Override
	public PaymentTradeReqDTO queryBatchOrderInfoBySid(Integer Sid) {
		PayTradeBatch tPayTradeBatch = payTradeBatchMapper.selectByPrimaryKey(Sid);
		PaymentTradeReqDTO tPaymentTradeReqDTO = new PaymentTradeReqDTO();
		tPaymentTradeReqDTO.setSid(tPayTradeBatch.getSid());
		tPaymentTradeReqDTO.setReqBatchNo(tPayTradeBatch.getReqBatchNo());
		tPaymentTradeReqDTO.setMerchantCode(tPayTradeBatch.getMerchantCode());
		tPaymentTradeReqDTO.setPayFlag(tPayTradeBatch.getPayFlag());
		tPaymentTradeReqDTO.setPayChannelId(tPayTradeBatch.getPayChannelId());
		tPaymentTradeReqDTO.setTradeNum(tPayTradeBatch.getTradeNum());
		tPaymentTradeReqDTO.setSumAmount(tPayTradeBatch.getSumAmount());
		tPaymentTradeReqDTO.setResultCode(tPayTradeBatch.getResultCode());
		tPaymentTradeReqDTO.setResultDesc(tPayTradeBatch.getResultDesc());
		tPaymentTradeReqDTO.setDealDate(tPayTradeBatch.getDealDate());
		tPaymentTradeReqDTO.setCreateDate(tPayTradeBatch.getCreateDate());
		tPaymentTradeReqDTO.setCreatorId(tPayTradeBatch.getCreatorId());
		return tPaymentTradeReqDTO;
	}

	@Override
	public PaymentTradeReqDTO queryTransPayRelateInfoByBatchId(Integer payBatchId) {
		RelaTradePaymentExample tRelaTradePaymentExample = new RelaTradePaymentExample();
		tRelaTradePaymentExample.createCriteria().andPayBatchIdEqualTo(payBatchId);
		List<RelaTradePayment> tRelaTradePaymentList = relaTradePaymentMapper.selectByExample(tRelaTradePaymentExample);
		PaymentTradeReqDTO tPaymentTradeReqDTO = new PaymentTradeReqDTO();
		tPaymentTradeReqDTO.setPayBatchId(tRelaTradePaymentList.get(0).getPayBatchId());
		tPaymentTradeReqDTO.setTradeId(tRelaTradePaymentList.get(0).getTradeId());
		tPaymentTradeReqDTO.setPayChannelId(tRelaTradePaymentList.get(0).getPayChannelId());
		tPaymentTradeReqDTO.setState(tRelaTradePaymentList.get(0).getState());
		tPaymentTradeReqDTO.setSendTime(tRelaTradePaymentList.get(0).getSendTime());
		tPaymentTradeReqDTO.setReturnTime(tRelaTradePaymentList.get(0).getReturnTime());
		tPaymentTradeReqDTO.setResultCode(tRelaTradePaymentList.get(0).getResultCode());
		tPaymentTradeReqDTO.setResultDesc(tRelaTradePaymentList.get(0).getResultDesc());
		tPaymentTradeReqDTO.setRemark(tRelaTradePaymentList.get(0).getRemark());
		return tPaymentTradeReqDTO;
	}

	@Override
	public PaymentTradeBatchDTO queryPaySumInfoByBatchNo(String payBatchNo) {
		PayTradePaymentBatchExample tPayTradePaymentBatchExample = new PayTradePaymentBatchExample();
		tPayTradePaymentBatchExample.createCriteria().andPayBatchNoEqualTo(payBatchNo);
		List<PayTradePaymentBatch> tPayTradePaymentBatchList = payTradePaymentBatchMapper.selectByExample(tPayTradePaymentBatchExample);
		if(tPayTradePaymentBatchList != null && tPayTradePaymentBatchList.size() > 0){
			PaymentTradeBatchDTO tPaymentTradeBatchDTO = new PaymentTradeBatchDTO();
			tPaymentTradeBatchDTO.setSid(tPayTradePaymentBatchList.get(0).getSid());
			tPaymentTradeBatchDTO.setPayBatchNo(tPayTradePaymentBatchList.get(0).getPayBatchNo());
			tPaymentTradeBatchDTO.setState(tPayTradePaymentBatchList.get(0).getState());
			tPaymentTradeBatchDTO.setIfStartDate(tPayTradePaymentBatchList.get(0).getIfStartDate());
			tPaymentTradeBatchDTO.setIfEndDate(tPayTradePaymentBatchList.get(0).getIfEndDate());
			tPaymentTradeBatchDTO.setIfContent(tPayTradePaymentBatchList.get(0).getIfContent());
			tPaymentTradeBatchDTO.setPayFlag(tPayTradePaymentBatchList.get(0).getPayFlag());
			tPaymentTradeBatchDTO.setPayChannelId(tPayTradePaymentBatchList.get(0).getPayChannelId());
			tPaymentTradeBatchDTO.setTradeAmount(tPayTradePaymentBatchList.get(0).getTradeAmount());
			tPaymentTradeBatchDTO.setTradeNum(tPayTradePaymentBatchList.get(0).getTradeNum());
			tPaymentTradeBatchDTO.setSendTime(tPayTradePaymentBatchList.get(0).getSendTime());
			tPaymentTradeBatchDTO.setReturnTime(tPayTradePaymentBatchList.get(0).getReturnTime());
			tPaymentTradeBatchDTO.setResultCode(tPayTradePaymentBatchList.get(0).getResultCode());
			tPaymentTradeBatchDTO.setResultDesc(tPayTradePaymentBatchList.get(0).getResultDesc());
			tPaymentTradeBatchDTO.setRemark(tPayTradePaymentBatchList.get(0).getRemark());
			return tPaymentTradeBatchDTO;
		}
		return null;
	}

    @Override
    public String queryStandBankCodeByBankCode(String bankCode) {
        PayCodeMappingExample tPayCodeMappingExample = new PayCodeMappingExample();
        tPayCodeMappingExample.createCriteria().andOriginalCodeEqualTo(bankCode);
        List<PayCodeMapping> tPayCodeMapList = payCodeMappingMapper.selectByExample(tPayCodeMappingExample);
        return tPayCodeMapList.get(0).getTargetCode();
    }

    @Override
    public String queryOfferFileNameByOfferPayChannelId(Integer payChannelId) {
        PayTradeOfferLogExample tPayTradeOfferLogExample = new PayTradeOfferLogExample();
        tPayTradeOfferLogExample.createCriteria().andPayChannelIdEqualTo(payChannelId).andOfferFileStatusEqualTo("1");
        List<PayTradeOfferLog> tPayTradeOfferLogList = payTradeOfferLogMapper.selectByExample(tPayTradeOfferLogExample);
        return tPayTradeOfferLogList.get(0).getOfferFile();
    }

    @Override
    public Integer queryPayTradeBatchSidByBatchNo(String reqBatchNo) {
        PayTradeBatchExample tPayTradeBatchExample = new PayTradeBatchExample();
        tPayTradeBatchExample.createCriteria().andReqBatchNoEqualTo(reqBatchNo);
        List <PayTradeBatch> tPayTradeBatchList = payTradeBatchMapper.selectByExample(tPayTradeBatchExample);
        return tPayTradeBatchList.get(0).getSid();
    }

    @Override
    public AllinpayRespDTO queryAllinpayTradeResult(AllinpayReqDTO tAllinpayReqDTO) {
        //1. 封装请求参数
        logger.info("----封装请求参数信息----");
        AllinpayQryReqDTO tAllinpayQryReqDTO = new AllinpayQryReqDTO();//查询订单信息
        AllinpayRespDTO tAllinpayRespDTO = new AllinpayRespDTO();
        tAllinpayQryReqDTO.setMERCHANT_ID(tAllinpayReqDTO.getMERCHANT_ID());
        tAllinpayQryReqDTO.setQUERY_SN(tAllinpayReqDTO.getQUERY_SN());
        tAllinpayQryReqDTO.setSTATUS(tAllinpayReqDTO.getSTATUS());
        tAllinpayQryReqDTO.setTYPE(tAllinpayReqDTO.getTYPE());
        tAllinpayQryReqDTO.setSTART_DAY(tAllinpayReqDTO.getSTART_DAY());
        tAllinpayQryReqDTO.setEND_DAY(tAllinpayReqDTO.getEND_DAY());
        logger.info("----获取具体的调用实现bean过程----");
        List<AllinpayRespDTO> tAllinpayResList = new ArrayList<AllinpayRespDTO>();
        try {
            InteraAllinPayQryApiService interPayApiService = (InteraAllinPayQryApiService)SpringContext.getBean("InterPayByPayTradeQuery");  //调用交易查询
            AllinpayQryRespDTO tPayInfoReturnList = new AllinpayQryRespDTO();
            try {
                logger.info("----调用API 获取返回结果信息----");
                 tPayInfoReturnList = interPayApiService.payRequest(tAllinpayQryReqDTO); //2.获取查询结果信息 
            } catch (SinoBaseException ee) {
                ee.printStackTrace();
                throw new SinoBizException(ee.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
                throw new SinoBizException("系统异常"+e.getMessage());
            }
            if (tPayInfoReturnList.getRET_CODE().equals("0000")) {  //成功的时候封装 并返回 相关信息
                if (tPayInfoReturnList.getAllinpayQryDetailsList().size()>0) {
                    List<AllinpayDetailsRespDTO>   tAllinpayDetailsList = new ArrayList<AllinpayDetailsRespDTO>();
                    for (int i = 0; i < tPayInfoReturnList.getAllinpayQryDetailsList().size(); i++) {  //转换并返回
                        AllinpayDetailsRespDTO tAllinpayDetailsDTO = new AllinpayDetailsRespDTO();
                        //tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getBATCHID()
                        tAllinpayDetailsDTO.setBATCHID(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getBATCHID());
                        tAllinpayDetailsDTO.setSN(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getSN());
                        tAllinpayDetailsDTO.setTRXDIR(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getTRXDIR());
                        tAllinpayDetailsDTO.setSETTDAY(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getSETTDAY());
                        tAllinpayDetailsDTO.setFINTIME(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getFINTIME());
                        tAllinpayDetailsDTO.setSUBMITTIME(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getSUBMITTIME());
                        tAllinpayDetailsDTO.setACCOUNT_NO(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getACCOUNT_NO());
                        tAllinpayDetailsDTO.setACCOUNT_NAME(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getACCOUNT_NAME());
                        tAllinpayDetailsDTO.setAMOUNT(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getAMOUNT());
                        tAllinpayDetailsDTO.setRET_CODE(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getRET_CODE());
                        tAllinpayDetailsDTO.setERR_MSG(tPayInfoReturnList.getAllinpayQryDetailsList().get(i).getERR_MSG());
                        tAllinpayDetailsList.add(tAllinpayDetailsDTO);
                    } 
                    tAllinpayRespDTO.setRET_CODE(tPayInfoReturnList.getRET_CODE());
                    tAllinpayRespDTO.setERR_MSG(tPayInfoReturnList.getERR_MSG());
                    tAllinpayRespDTO.setAllinpayDetailsList(tAllinpayDetailsList);
                } else {
                    tAllinpayRespDTO.setRET_CODE(tPayInfoReturnList.getRET_CODE());
                    tAllinpayRespDTO.setERR_MSG(tPayInfoReturnList.getERR_MSG());
                }
            }else {
                tAllinpayRespDTO.setRET_CODE(tPayInfoReturnList.getRET_CODE());
                tAllinpayRespDTO.setERR_MSG(tPayInfoReturnList.getERR_MSG());
            }
            
        }catch (Exception e) {
            e.printStackTrace();
            throw new SinoBizException("系统异常"+e.getMessage());
        }
        return tAllinpayRespDTO;
    }

    @Override
    public PageGrid queryPaymentTradePageList(PageQueryModel cPageQueryModel) {
        PageGrid tPageGrid = new PageGrid();
        try {
           
            tPageGrid = payTradePageMapper.findPageGrid(cPageQueryModel);
            //分页查询 并获取 商户名称 渠道名称
            List<PayTradeMainReturnDTO> payThirdpayMainPagelist = tPageGrid.getRows();
            if (tPageGrid.getSize()>0) {
                for (int i = 0; i < payThirdpayMainPagelist.size(); i++) {
                    PayTradeMainReturnDTO tPayTradeMainReturnDTO = new PayTradeMainReturnDTO();
                    if (payThirdpayMainPagelist.get(i).getMerchantCode()==null||payThirdpayMainPagelist.get(i).getMerchantCode().equals("")) {
                        tPayTradeMainReturnDTO.setMerchantName("");
                    }else {
                        MerchantSignInfo tMerchantSignInfo = merchantInfoFacadeService.getMerchantByCode(payThirdpayMainPagelist.get(i).getMerchantCode());
                        tPayTradeMainReturnDTO.setMerchantName(tMerchantSignInfo.getMerchantName());
                    }
                    if (payThirdpayMainPagelist.get(i).getThirdpayCode()==null||payThirdpayMainPagelist.get(i).getThirdpayCode().equals("")) {
                        tPayTradeMainReturnDTO.setThirdpayName("");
                    }else {
                        PayThirdpayDTO tPayThirdpayDTO = payChannelConfigFacadeService.queryPayThirdpayMainByThirdpayCode(payThirdpayMainPagelist.get(i).getThirdpayCode());
                        tPayTradeMainReturnDTO.setThirdpayName(tPayThirdpayDTO.getThirdpayName());
                    }
                    tPayTradeMainReturnDTO.setSid(payThirdpayMainPagelist.get(i).getSid());
                    tPayTradeMainReturnDTO.setSerialNo(payThirdpayMainPagelist.get(i).getSerialNo());
                    tPayTradeMainReturnDTO.setBizNo(payThirdpayMainPagelist.get(i).getBizNo());
                    tPayTradeMainReturnDTO.setPayNo(payThirdpayMainPagelist.get(i).getPayNo());
                    tPayTradeMainReturnDTO.setMerchantCode(payThirdpayMainPagelist.get(i).getMerchantCode());
                    tPayTradeMainReturnDTO.setThirdpayCode(payThirdpayMainPagelist.get(i).getThirdpayCode());
                    tPayTradeMainReturnDTO.setPayStatus(payThirdpayMainPagelist.get(i).getPayStatus());
                    tPayTradeMainReturnDTO.setPayDate(payThirdpayMainPagelist.get(i).getPayDate());
                    payThirdpayMainPagelist.set(i, tPayTradeMainReturnDTO);
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tPageGrid;
    }


	@Override
	public  PaymentInfoDetailDTO queryBatchResultCode(PayTradeSearchDTO payTradeSearchDTO){
		return payTradeMapper.selectBatchMsgFotSearch(payTradeSearchDTO);
	}

	@Override
	public List<PaymentInfoDetailDTO> queryAllPayTransInfoForBatchNo(String batchNo) {
		//查询当日所有支付交易信息表数据 请求参数 渠道编码、当日
		List<PaymentInfoDetailDTO> tPaymentTradeReqList = payTradeMapper.selectBatchNoTransInfo(batchNo);
		return tPaymentTradeReqList;
	}
}
